<template>
	<view class="sq-upload" :style="{display:'inline-block'}" @tap="upload">
		<view class="flex-box">
			 <progress v-if="progress >0 && progress<100" class="progress" :percent="progress"  stroke-width="3" />
		</view>
		<slot name="content"></slot>
	</view>
</template>

<script>
	let uploadFileSize = 1024*1024*30
	let OSSParams;
	export default {
		props:{
			item:{
				type:Object,
				default(){
					return {}
				},
			},
			index:{
				type:Number,
				default:0,
			},
			readonly:{
				type:[String,Boolean],
				default:false
			},
			type:{
				type:String,
				default:'image'
			},
			cameraList: [{
					value: 'back',
					name: '后置摄像头',
					checked: 'true'
				},
				{
					value: 'front',
					name: '前置摄像头'
				},
			],
			cameraIndex: 0,
			count:{
				type:Number,
				default:1,
			},
		},
		data() {
			return {
				progress:0,
				disabled:false
			};
		},
		methods: {
			upload() {
				var that = this;
				if(that.readonly)return
				if(that.disabled )return
				that.disabled = true;
				setTimeout(()=>{
					that.disabled = false;
				},3000)
				var emitFilePath = ''
				if(this.type == 'image'){
					uni.chooseImage({
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album','camera'], //从相册选择
						count:that.count,
					    success: (chooseImageRes) => {
 							let len = chooseImageRes.tempFilePaths.length
							uni.hideToast()
							let total = 0;
							for (var i = 0; i <= (len - 1); i++) {
								debugger
								total = i
								let tempFiles = chooseImageRes.tempFiles[i]
								let tempFilePaths = chooseImageRes.tempFilePaths[i]
								if (tempFiles.size > uploadFileSize) {
									uni.showToast({
										title: '文件大小超过系统上传限制：' + uploadFileSize/1024/1024+'M',
										icon: 'none',
										duration: 1000
									});
									return;
								}
								uni.showLoading({
									title: '文件上传中'
								});
								
								that.updateAvatarFun(tempFilePaths)
								
							}
							
					        
					    }
					});
				}else{
					// 上传视频
					const uploadTask =uni.chooseVideo({
						maxDuration:60,
						count: 1,
						camera: 'front',
						sourceType: ['album'],
						success: (responent) => {
							if (responent.size > uploadFileSize) {
								uni.showToast({
									title: '文件大小超过系统上传限制：' + uploadFileSize/1024/1024+'M',
									icon: 'none',
									duration: 1000
								});
								return;
							}
							let videoFile = responent.tempFilePath;
							uni.showLoading({
								title: '文件上传中'
							});
							that.updateAvatarFun(tempFilePaths)
						}
					})
				}
				
				
			},
			// 上传头像函数
			updateAvatarFun(tempFilePaths){
				uni.showLoading({
					title: '上传中',
					mask: true
				});
				let Authorization = 'Bearer ' + uni.getStorageSync('Authorization')
				const header = {
					Authorization: Authorization,
					'Accept-Version': '1.0.0'
				}
				let _this = this;
				uni.uploadFile({
					url:'/api/app/common/upload_img',
					filePath: tempFilePaths,
					name: 'file',
					header,
					success: (uploadFileRes) => {
						debugger
						// this.$emit('upload', 'https://zxcity-app.oss-cn-hangzhou.aliyuncs.com/user-dir/zxnttJMsKFAmw2hTjbF4zHsreM.png')
						// 如果图片名称为image.jpg会有显示的问题，可能image会默认关键字
						console.log(uploadFileRes)
						uni.hideLoading()
						try{
							console.log(JSON.parse(uploadFileRes.data))
							if(JSON.parse(uploadFileRes.data).code == 200){
								uni.showToast({
									title:'上传成功！',
									icon:'none'
								})
								this.$emit('upload', JSON.parse(uploadFileRes.data).data)
							} else {
								uni.showToast({
									title:'请上传标准的身份证头像面图片！'
								})
							}
						}catch(e){
							//TODO handle the exception
						}
						
					}
				});
			},
			
		},
	}
</script>

<style lang="scss">
	.sq-upload{
		position: relative;
	}
	.flex-box{
		progress{
			position: absolute;
			top: 50%;
			left: 0;
			width: 100%;
			z-index: 99;
		}
	}
</style>
